package com.koron.order.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.change.dispatch.mapper.ChangeOrderWorkDeptMapper;
import com.koron.order.change.util.ThreadLocalOrderChange;
import com.koron.order.common.bean.base.OrderType;
import com.koron.order.common.bean.po.OrderWorkDeptPo;
import com.koron.order.common.mapper.OrderWorkDeptMapper;
import com.koron.order.common.service.OrderWorkDeptService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工单作业单位(OrderWorkDept)表服务实现类
 *
 * @author zhouj
 * @since 2022-02-17 15:04:46
 */
@Service("orderWorkDeptService")
public class OrderWorkDeptServiceImpl implements OrderWorkDeptService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, OrderWorkDeptPo orderWorkDept) {

        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);

        return mapper.insertOrUpdate(orderWorkDept);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<OrderWorkDeptPo> orderWorkDeptList) {

        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);

        return mapper.insertOrUpdateBatch(orderWorkDeptList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderWorkDeptPo orderWorkDept) {

        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);

        return mapper.insert(orderWorkDept);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<OrderWorkDeptPo> orderWorkDeptList) {

        if (CollectionUtils.isEmpty(orderWorkDeptList)) {
            return 0;
        }

        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);

        //先根据工单id删除再保存
        mapper.deleteByOrderId(orderWorkDeptList.get(0).getOrderId());

        return mapper.insertBatch(orderWorkDeptList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderWorkDeptPo orderWorkDept) {
        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);
        return mapper.update(orderWorkDept);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);
        return mapper.deleteByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("deleteByDispatchId")
    public int deleteByDispatchId(SessionFactory factory, String dispatchId) {
        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);
        return mapper.deleteByDispatchId(dispatchId);
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderWorkDeptPo queryById(SessionFactory factory, String id) {

        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryByOrderIdTeamId")
    public List<OrderWorkDeptPo> queryByOrderIdTeamId(SessionFactory factory, String orderId, String teamId) {
        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);
        OrderWorkDeptPo orderWorkDeptPo = new OrderWorkDeptPo();
        orderWorkDeptPo.setOrderId(orderId);
        orderWorkDeptPo.setTeamId(teamId);
        List<OrderWorkDeptPo> list = mapper.queryList(orderWorkDeptPo);

        return list;
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<OrderWorkDeptPo> queryByOrderId(SessionFactory factory, String orderId) {

        return queryByOrderIdTeamId(factory, orderId, null);
    }

    @Override
    public List<OrderWorkDeptPo> queryByOrderIds(SessionFactory factory, List<String> orderIds) {
        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);

        return mapper.queryByOrderIds(orderIds);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<OrderWorkDeptPo> queryList(SessionFactory factory, OrderWorkDeptPo orderWorkDept) {

        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);

        List<OrderWorkDeptPo> list = mapper.queryList(orderWorkDept);

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, OrderWorkDeptPo orderWorkDept, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        OrderWorkDeptMapper mapper = getOrderWorkDeptMapper(factory);

        List<OrderWorkDeptPo> list = mapper.queryList(orderWorkDept);

        return new PageInfo(list);
    }

    OrderWorkDeptMapper getOrderWorkDeptMapper(SessionFactory factory) {

        if (OrderType.CHANGE.equals(ThreadLocalOrderChange.get())) {
            return factory.getMapper(ChangeOrderWorkDeptMapper.class);
        }
        return factory.getMapper(OrderWorkDeptMapper.class);
    }

}